package cn.youyeku.zsxh.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.LogUtils;


@SuppressWarnings("unchecked")
public abstract class RefreshFootAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    protected List<T> mData=new ArrayList<>();
    //上拉加载更多
    public final int STATUS_UP_LOAD_MORE = 0;
    //正在加载中
    public final int STATUS_LOADING = 1;
    public final int STATUS_NOT_MORE_DATA = 2;


    private int STATUS_LOADING_NOW = STATUS_UP_LOAD_MORE;

    private final int TYPE_ITEM = 1;
    private final int TYPE_FOOTER = 0;

    //over than maxItem show footer
    public static int maxViewItem = 15;
    protected int mFooterR = -1;
    protected int mItemR;

    protected abstract void onBindCustomerData(RefreshViewHolder holder, T data, int pos);
    protected abstract void setmOnItemClick(View itemView,T data,  int pos);

    public RefreshFootAdapter( int mItemR, int mFooterR) {
        this.mFooterR = mFooterR;
        this.mItemR = mItemR;

    }


    public void changeStatus(int status) {
        STATUS_LOADING_NOW = status;

    }


    public void addMoreItem(List<T> data) {
        LogUtils.w(String.valueOf(getItemCount()));
        //判断对象不为空 不重复数据
        if (data!=null &&data.size()>0) {
            mData.clear();
            for(T d : data){
                if(!mData.contains(d)){
                    mData.add(d);
                }
            }

            notifyDataSetChanged();
        }

    }


    @Override
    public int getItemViewType(int position) {

        if (mFooterR != -1 && position == getItemCount() - 1) {

            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            return new RefreshViewHolder(LayoutInflater.from(parent.getContext()).inflate(mFooterR, parent, false));
        }
        View layout = LayoutInflater.from(parent.getContext()).inflate(mItemR, parent, false);
        return new RefreshViewHolder(layout);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


        RefreshViewHolder mHolder = (RefreshViewHolder) holder;
        switch (getItemViewType(position)) {
            case TYPE_FOOTER:
                onBindFooterView(mHolder);
                break;
            case TYPE_ITEM:
                onBindCustomerData((RefreshViewHolder) holder, getData(position), position);
                setmOnItemClick(mHolder.itemView, getData(position), position);
                break;
        }
    }


    public T getData(int pos) {
        return mData.get(pos);
    }

    @Override
    public int getItemCount() {

        if (mData.size()==0) {
            return 0;
        } else {
            return mData.size() + 1;
        }
    }

    protected void onBindFooterView(RefreshViewHolder refreshViewHolder) {
        TextView footer = refreshViewHolder.getChildView(R.id.refresh_footer);
        ProgressBar progressBar = refreshViewHolder.getChildView(R.id.refresh_progressbar);
        LinearLayout ly = refreshViewHolder.getChildView(R.id.ly_set);
        if (getItemCount() < maxViewItem) {
            ly.setVisibility(View.GONE);
            return;
        }

        switch (STATUS_LOADING_NOW) {
            case STATUS_UP_LOAD_MORE:
                footer.setText(R.string.refresh_footer_pull_load_more);
                progressBar.setVisibility(View.VISIBLE);
                break;
            case STATUS_LOADING:
                footer.setText(R.string.refresh_footer_loading);
                progressBar.setVisibility(View.VISIBLE);
                break;
            case STATUS_NOT_MORE_DATA:
                footer.setText(R.string.refresh_footer_not_more_data);
                progressBar.setVisibility(View.GONE);
                break;
        }
        ly.setVisibility(View.VISIBLE);


    }


    public class RefreshViewHolder extends RecyclerView.ViewHolder {

        public RefreshViewHolder(View itemView) {
            super(itemView);
        }

        public <T extends View> T getChildView(int id) {
            return (T) this.itemView.findViewById(id);
        }
    }
}
